#include "sysinfothread.h"

#include <qprocess.h>
#include <QDebug>
#include <qapplication.h>
#include "tool.h"


SysInfoThread::SysInfoThread(QObject *parent)
{
}

void SysInfoThread::run()
{
    QString cmd = "top -b -n 1 | grep 'Cpu(s)' | awk '{print 100-$8}' ";
    QString cmd2 = "free | awk 'NR==2{print $4/1024}'";
    QString cmd3 = "cat /sys/class/thermal/thermal_zone0/temp | awk '{printf \"%.1f\\n\", $1/1000}'";
    cpuUsed = execShell(cmd);
    memFree = execShell(cmd2);
    cpuTemp = execShell(cmd3);
    bool ok;
    double temp = cpuTemp.toDouble(&ok);
    int stdCpuTemp = 85;
    if(ok){
        if(getSetting("cpuTemp").toInt() != 0){
            stdCpuTemp = getSetting("cpuTemp").toInt();
        }
        if(temp > stdCpuTemp){
            emit tempError("CPU温度过高："+cpuTemp+"℃");
        }
    }else{
        cpuTemp = "None";
        emit tempError("CPU温度获取错误："+cpuTemp);
    }
//    qDebug()<<"CPU idle:"<<cpuUsed<<"MEM free:"<<memFree<<"CPU Temp:"<<cpuTemp;
}
